//Source file: D:\\doc\\workflow\\src\\com\\browsesoft\\PropertiesFile.java
package com.browsesoft;

import java.io.File;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * XML格式表示的属性文件
 * 
 * @author Browsesoft
 * @version 1.0
 */
public class PropertiesFile {
	/**
	 * 属性文件的XML文档
	 */
	private Document document = null;

	private File file;

	/**
	 * 使用属性文件文件名构造
	 * 
	 */
	public PropertiesFile(String fileName) throws Exception {
		// 生成Document
		// ClassLoader loader = Thread.currentThread().getContextClassLoader();
		InputStream is = PropertiesFile.class.getResourceAsStream(fileName);
		DocumentBuilder builder = DocumentBuilderFactory.newInstance()
				.newDocumentBuilder();
		document = builder.parse(is);
	}

	/**
	 * 得到对象属性
	 * 
	 * @param objName
	 *            对象名
	 * @param propertyName
	 *            属性名
	 * @return 属性值
	 */
	public String getProperty(String objName, String propertyName) {
		// 取得对象列表
		NodeList nl = document.getElementsByTagName(objName);
		if (nl.getLength() != 1) {
			throw new PropertiesNotFoundException(objName, propertyName);
		}
		// 取第一个对象
		Element e = (Element) nl.item(0);
		if (e.hasAttribute(propertyName)) {
			String result = e.getAttribute(propertyName);
			return result;
		} else {
			throw new PropertiesNotFoundException(objName, propertyName);
		}
	}
	
	/**
	 * 得到对象属性，如果不存在，使用默认值
	 */
	public String getProperty(String objName, String propertyName, String init) {
		try {
			String result = this.getProperty(objName, propertyName);
			return result;
		} catch (PropertiesNotFoundException e) {
			// 属性不存在，返回默认值
			return init;
		}
	}

	/**
	 * 得到对象属性
	 * 
	 * @param objName
	 *            对象名
	 * @param propertyName
	 *            属性名
	 * @return 属性值
	 */
	public String getProperty(String objName, String propertyName, int postion)
			throws Exception {
		// 取第postion个对象
		Element e = getElement(objName, postion);
		if (e.hasAttribute(propertyName)) {
			String result = e.getAttribute(propertyName);
			return result;
		} else {
			throw new Exception("属性文件错误" + objName + "的属性" + propertyName
					+ "不存在");
		}
	}

	public Element getElement(String objName, int postion) throws Exception {
		// 取得对象列表
		NodeList nl = document.getElementsByTagName(objName);
		if (nl.getLength() < postion) {
			throw new Exception("属性文件错误" + objName + "没有第" + postion + "项");
		}
		// 取第postion个对象
		Element e = (Element) nl.item(postion);
		return e;
	}

	/**
	 * 得到对象属性
	 * 
	 * @param objName
	 *            对象名
	 * @param propertyName
	 *            属性名
	 * @return 属性值
	 */
	public String getPropertyForInvestment(String objName, String propertyName)
			throws Exception {
		String result = "";
		// 取得对象列表
		NodeList nl = document.getElementsByTagName(objName);
		if (nl.getLength() > 0) {
			// 取第一个对象
			Element e = (Element) nl.item(0);
			if (e.hasAttribute(propertyName)) {
				result = e.getAttribute(propertyName);
			}
		}
		return result;
	}

	/**
	 * 取得文档对象
	 * 
	 * @return 文档对象
	 */
	public Document getDocument() {
		return document;
	}

	public File getFile() {
		return file;
	}
}
